<?php
$page_title = "Sondaj - Fifaonline.ro | Campionate fifa virtuale | Stiri sportive";
include_once("top.php");
?>
<?php
require_once ("/includes/mysql_connect.php"); //Realizeaza conexiunea la baza de date.
if(isset($_GET['group'])){
	$group_name = $_GET['group'];
	$request = "SELECT * FROM " .$group_name. "";
	$result = mysql_query($request);
	if(mysql_num_rows($result) == 0){ 
		$group_name = FALSE;
		$alert = "Acest id nu exista";
	}
	else{
		$alert = FALSE;
	}
}
else{
	$group_name = FALSE;
}
if(isset($_POST["submitted"])){
	require_once("/includes/mysql_connect.php");
	$query = "SELECT id, type, data, votes, percent FROM ".$group_name." WHERE data = '".$_POST[$group_name]."'";
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);
	$voturi = $row['votes'];
	$voturi += 1;
	$query = "UPDATE ".$group_name. " SET votes = $voturi WHERE data = '".$_POST[$group_name]."'"; #Plus un vot
	$result = mysql_query($query);
	$query = "SELECT SUM(votes) FROM ".$group_name. " WHERE type = 1"; #Suma voturilor
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);
	$q = $row['SUM(votes)'];
	$query = "UPDATE ".$group_name. " SET votes = $q WHERE type = 0"; #Introdu suma voturilor
	$result = mysql_query($query);
	$query = "SELECT votes FROM ".$group_name. " WHERE type = 1";
	$result = mysql_query($query);
	$num_rows = mysql_num_rows($result)+1;
	for($i = 2; $i <= $num_rows; $i++){
		$query = "SELECT votes FROM ".$group_name. " WHERE id = $i";
		$result = mysql_query($query);
		$row = mysql_fetch_array($result);
		$a[$i] = $row['votes'];
		$x[$i] = ($a[$i] * 100)/$q;
		$query = "UPDATE ".$group_name. " SET percent = $x[$i] WHERE id = $i";
		$result = mysql_query($query);
		$_SESSION[$group_name] = $group_name ;
	}
}
else
?>
<script type="text/javascript">

function enableElement()
{
document.getElementById("sbmt").disabled=false;
}

</script>
<?php
$query = "SELECT id, type, data FROM ".$group_name." WHERE type = 0";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$file = substr($group_name, 5);
$filename = 'C:/wamp/www/uploads/'.$file.'poll.jpg';
echo "<p class = 'text3'>";
if (file_exists($filename))
		{
			echo "<img src='".$filename."' align='right' width='100' height='140'>".$row['data']."</p>";
		}
		else
		{
			echo $row['data'];
		}

if(!isset($_POST["submitted"]) && !isset($_SESSION[$group_name])){
	echo "<form name = '".$group_name."' action='pollgroup.php?group=".$group_name."' method='post'/>";
	$query = "SELECT id, type, data FROM ".$group_name." WHERE type = 1";
	$result = mysql_query($query);
	while ($row = mysql_fetch_array($result)){
		echo "<p class = 'text4'><input type = 'radio' onchange = 'enableElement()' name = '". $group_name . "' value = '".$row['data']."'> ".$row['data']. "</p><br>";
	}
	echo "<input type = 'submit' name = 'submit' value = 'Vote!' disabled='disabled' id='sbmt'/>";
	echo "<input type=\"hidden\" name=\"submitted\" value=\"TRUE\"  />";
	echo "</form>";
}
else{
	$query = "SELECT id, type, data FROM ".$group_name." WHERE type = 1";
	$result = mysql_query($query);
	while ($row = mysql_fetch_array($result)){
		$query = "SELECT id, percent, data, votes FROM ".$group_name."  WHERE type=1";
		$result = mysql_query($query);
		while ($row = mysql_fetch_array($result)){
			$id = $row['id']-1;
			$width = $row['percent'] * 2;
			$votes = $row['votes'];
			echo "<p class = 'text4'>".$row['data']." (";
			echo $width/2;
			echo "%) <small> ".$votes." voturi</small> </p><br>";
			echo "<p><img src='/images/bar".$id.".png' height='25' width='$width'></p><br>";
		}
	}
}
	
include_once("end.php");
?>